/*
Philip Dilse
dilsep
Assignment 3

unit test for scoreFor()
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "dominion.h"
#include "dominion_helpers.h"

int cardScore(int card) {
    if (card == curse)
        return -1;
    else if (card == estate || card == great_hall)
        return 1;
    else if (card == duchy)
        return 3;
    else if (card == province)
        return 6;
    else
        return 0;
}

int main() {
    int i, j;
    int randomCard;
    int numPlayers = 4;
    int expectedScore, calcScore;
    int player;
    int numGardens;
    int totalCards;
    struct gameState *gs = newGame();
    gs->numPlayers = numPlayers;
 
    // set up random number gen
    srand(time(NULL));

    for (player = 0; player < numPlayers; player++) {
        for (i = 1; i <= MAX_HAND + 1; i++) {
            expectedScore = 0;
            numGardens = 0;

            // randomly pick counts for three stacks
            gs->handCount[player] = rand() % i;
            gs->discardCount[player] = rand() % i;
            gs->deckCount[player] = rand() % i;

            // randomly assign cards to hand
            for (j = 0; j < gs->handCount[player]; j++) {
                randomCard = rand() % (treasure_map + 1);
                gs->hand[player][j] = randomCard;
                expectedScore += cardScore(randomCard);
                if (randomCard == gardens)
                    numGardens++;
            }
            // randomly assign cards to discard
            for (j = 0; j < gs->discardCount[player]; j++) {
                randomCard = rand() % (treasure_map + 1);
                gs->discard[player][j] = randomCard;
                expectedScore += cardScore(randomCard);
                if (randomCard == gardens)
                    numGardens++;
            }
   
            // randomly assign cards to deck
            for (j = 0; j < gs->deckCount[player]; j++) {
                randomCard = rand() % (treasure_map + 1);
                gs->deck[player][j] = randomCard;
                expectedScore += cardScore(randomCard);
                if (randomCard == gardens)
                    numGardens++;
            }

            totalCards = gs->handCount[player] + gs->discardCount[player] + gs->deckCount[player];
            expectedScore += numGardens * (totalCards / 10);
            
            // get scoreFor() 
            calcScore = scoreFor(player, gs);
            if (expectedScore != calcScore) 
                printf("scoreFor() error: expected=%d calculated=%d\n", expectedScore, calcScore);
        }
    }
    free(gs);
    return 0;
}
